module.exports = Behavior({
    data: {
        page: 1,
        list: [],
        empty: true,
        loading: false,
        finished: false,
        triggered: false,
        login: false, //不需要登录,页面可覆盖
        searchText: '', //搜索文本
    },
    methods: {
        getParamsData() { //页面特殊的请求参数，需要时在页面内覆盖此方法，默认返回{}
            return {}
        },
        getReqUrl() { //增加一个方法改变url请求参数,需要时在页面内覆盖此方法(this.data.reqUrl不存在时生效)
        },
        //获取列表
        getList(refresh) {
            this.setData({ loading: true })
            let { reqUrl, usePrivateMethod, login } = this.data
            if (refresh) {
                this.setData({
                    finished: false,
                    empty: true,
                    list: [],
                })
                this.data.page = 1
            }
            let paramsData = this.getParamsData()
            this.request({
                url: reqUrl || this.getReqUrl(),
                login,
                data: {
                    page: this.data.page,
                    limit: 10,
                    ...paramsData
                }
            }, async (res) => {
                let pushObj = {}
                if (refresh) this.setData({ empty: true, list: [] })
                let length = this.data.list.length

                let result = res.data

                if (usePrivateMethod) {
                    result = await this.privateMethod(refresh, res) //页面私有方法(可以格式化数据)
                }

                for (let item of result) {
                    Reflect.set(pushObj, ['list[' + length + ']'], item)
                    length++
                }

                Object.assign(pushObj, { loading: false, empty: result.length === 0 && this.data.list.length === 0 })

                if (res.current_page === res.pages) Object.assign(pushObj, { finished: true })


                if (refresh) {
                    Object.assign(pushObj, { triggered: false })
                    wx.stopPullDownRefresh()
                }

                this.setData(pushObj)
            }, () => {
                if (refresh) {
                    wx.stopPullDownRefresh()
                    this.setData({ triggered: false })
                }
            })
        },
        onRefresh() { //scroll-view下拉刷新
            if (this.data.triggered) return
            this.setData({ triggered: true })
            this.getList(true)
        },
        onLoadMore() { //scroll-view加载更多
            if (this.data.loading || this.data.finished) return
            this.data.page++
            this.getList()
        },
        onPullDownRefresh: function() { //页面下拉刷新
            this.getList(true)
        },
        onReachBottom: function() { //页面加载更多
            if (this.data.loading || this.data.finished) return
            this.onLoadMore()
        },
        searchTextChange({ detail }) { //如果有搜索框
            this.setData({ searchText: detail })
            detail === '' && this.onSearch() //清空内容自动请求
        },
        onSearch() {
            this.setData({
                list: [],
                empty: true
            })
            this.getList(true)
        }
    }
})